//
//  UserInfo.m
//  KuaiTu
//
//  Created by 代码女神 on 2018/11/21.
//  Copyright © 2018年 WeiJiang. All rights reserved.
//

#define KEY_LoginStatus @"loginStatus"
#define KEY_club_name @"club_name"
#define KEY_userId @"userId"
#define KEY_birthday @"birthday"
#define KEY_type_name @"type_name"
#define KEY_address @"address"
#define KEY_selfdom @"selfdom"
#define KEY_xingzuo @"xingzuo"
#define KEY_sex @"sex"
#define KEY_pic @"pic"
#define KEY_nick_name @"nick_name"
#define KEY_school_name @"school_name"
#define KEY_yx_name @"yx_name"
#define KEY_zy_name @"zy_name"
#define KEY_add_school_time @"add_school_time"
#define KEY_education @"education"

#import "UserInfo.h"


@implementation UserInfo

static UserInfo *_user = nil;
+ (UserInfo *)sharedUser{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _user = [[UserInfo alloc] init];
    });
    return _user;
}

//+ (NSDictionary *)objectClassInArray{
//    return @{
//             @"label" : @"KTTagModel"
//             };
//}

- (void)saveUserInfoToSanbox{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.nick_name forKey:KEY_nick_name];
    [defaults setObject:self.userId forKey:KEY_userId];
    [defaults setObject:self.sex forKey:KEY_sex];
    [defaults setObject:self.xingzuo forKey:KEY_xingzuo];
    [defaults setObject:self.pic forKey:KEY_pic];
    [defaults setObject:self.selfdom forKey:KEY_selfdom];
    [defaults setObject:self.address forKey:KEY_address];
    [defaults setObject:self.birthday forKey:KEY_birthday];
    [defaults setObject:self.club_name forKey:KEY_club_name];
    [defaults setObject:self.type_name forKey:KEY_type_name];
    [defaults setObject:self.school_name forKey:KEY_school_name];
    [defaults setObject:self.yx_name forKey:KEY_yx_name];
    [defaults setObject:self.zy_name forKey:KEY_zy_name];
    [defaults setObject:self.add_school_time forKey:KEY_add_school_time];
    [defaults setObject:self.education forKey:KEY_education];
    //登录返回
    [defaults setObject:self.user_id forKey:KEY_user_id];

    [defaults synchronize];
}


- (void)loadUserInfoFromSanbox{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.nick_name = [defaults objectForKey:KEY_nick_name];
    self.userId = [defaults objectForKey:KEY_userId];
    self.sex = [defaults objectForKey:KEY_sex];
    self.xingzuo = [defaults objectForKey:KEY_xingzuo];
    self.pic = [defaults objectForKey:KEY_pic];
    self.selfdom = [defaults objectForKey:KEY_selfdom];
    self.address = [defaults objectForKey:KEY_address];
    self.birthday = [defaults objectForKey:KEY_birthday];
    self.club_name = [defaults objectForKey:KEY_club_name];
    self.type_name = [defaults objectForKey:KEY_type_name];
    self.school_name = [defaults objectForKey:KEY_school_name];
    self.yx_name = [defaults objectForKey:KEY_yx_name];
    self.zy_name = [defaults objectForKey:KEY_zy_name];
    self.add_school_time = [defaults objectForKey:KEY_add_school_time];
    self.education = [defaults objectForKey:KEY_education];
    //登录返回
    self.user_id = [defaults objectForKey:KEY_user_id];

}


- (void)cleanUserInfo{
    self.nick_name = @"";
    self.sex = @"";
    self.xingzuo = @"";
    self.pic = @"";
    self.selfdom = @"";
    self.address = @"";
    self.birthday = @"";
    self.club_name = @"";
    self.type_name = @"";
    self.school_name = @"";
    self.yx_name = @"";
    self.zy_name = @"";
    self.add_school_time = @"";
    self.education = @"";
    self.userId = @"";
    //登录数据
    self.user_id = @"";

    [self saveUserInfoToSanbox];
}
//- (void)setToken:(NSString *)token{
//    if (token.length>0 && ![token isEqualToString:_token]) {
//        [JPUSHService setTags:nil alias:token fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
//            NSLog(@"%d-------------%@,-------------%@",iResCode,iTags,iAlias);
//        }];
//    }
//    _token = token;
//}

- (void)setWithDic:(NSDictionary *)dic{
    [self mj_setKeyValues:dic];
    [self saveUserInfoToSanbox];
}

+ (NSString *)getUID{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:KEY_user_id]) {
        return [[NSUserDefaults standardUserDefaults] objectForKey:KEY_user_id];
    }
    return @"";
}

- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"ImageFile"];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath]) {
        [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    }
    //压缩图片
    NSData* imageData = UIImagePNGRepresentation(tempImage);
    
    // 获取图片全路径
    NSString* fullPathToFile = [filePath stringByAppendingPathComponent:imageName];
    
    // 写入图片
    [imageData writeToFile:fullPathToFile atomically:NO];
}

- (void)removeImageFile {
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"ImageFile"];
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}

+ (void)Log:(UIViewController *)viewController{

//    KTLoginViewController *log = [[KTLoginViewController alloc] init];
//    BaseNavigationVC *vc = [[BaseNavigationVC alloc] initWithRootViewController:log];
//    [viewController presentViewController:vc animated:YES completion:nil];

}

+ (void)alertLogin:(UIViewController *)viewController{
    NSString *uid = UID;
    if (uid.length>0) {
        return;
    }
    UIAlertController *ctrl = [UIAlertController alertControllerWithTitle:@"您还未登录,请先登录!" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        KTLoginViewController *log = [[KTLoginViewController alloc] init];
//        BaseNavigationVC *vc = [[BaseNavigationVC alloc] initWithRootViewController:log];
//        [viewController presentViewController:vc animated:YES completion:nil];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [ctrl addAction:sure];
    [ctrl addAction:cancel];
    [viewController presentViewController:ctrl animated:YES completion:nil];
}

@end
